package com.zzu.chat.chat;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.zzu.chat.R;
import com.zzu.chat.bean.Blink;
import com.zzu.chat.chat.detail.Chat_CommentActivity;

import java.util.List;

public class BlinkAdapter extends RecyclerView.Adapter<BlinkAdapter.ViewHolder>{

    private List<Blink> mBlinkList;

    private Context mContext;
    class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        TextView blinkcontent;
        TextView blinktopic;
        TextView blinkusername;

        public ViewHolder (View view){
            super(view);
            cardView = view.findViewById(R.id.blink_card_view);
            blinkcontent = (TextView) view.findViewById(R.id.tv_content);
            blinktopic = (TextView) view.findViewById(R.id.tv_tag);
            blinkusername = (TextView) view.findViewById(R.id.tv_user_name);
        }
    }

    public BlinkAdapter(List<Blink> blinkList){
        this.mBlinkList = blinkList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_news_card_view_list_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        if (mContext==null){
            mContext = parent.getContext();
        }
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转
                int position = holder.getAdapterPosition();
                Blink blink = mBlinkList.get(position);
                Intent intent = new Intent(mContext, Chat_CommentActivity.class);
                intent.putExtra("blinkObjectId", blink.getObjectId());
                mContext.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Blink blink = mBlinkList.get(position);
        holder.blinkcontent.setText(blink.getContent());
        holder.blinktopic.setText(blink.getTopic());
        holder.blinkusername.setText(blink.getUsername());
    }


    @Override
    public int getItemCount(){
        return mBlinkList.size();
    }
}

